package com.hengli.controller.admin;

import com.hengli.dto.WinepackageDTO;
import com.hengli.dto.WinepackagePageQueryDTO;
import com.hengli.entity.Winepackage;
import com.hengli.result.PageResult;
import com.hengli.result.Result;
import com.hengli.service.WinepackageService;
import com.hengli.vo.WinepackageVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/winepackage")
@Slf4j
@Api(tags = "套餐相关接口")
@CrossOrigin
public class WinepackageController {

    @Autowired
    private WinepackageService winepackageService;

    /**
     * 新增套餐
     * @param winepackageDTO
     * @return
     */
    @PostMapping
    @ApiOperation("新增套餐")
    @CacheEvict(cacheNames = "winepackageCache", key = "#winepackageDTO.categoryId")
    public Result save(@RequestBody WinepackageDTO winepackageDTO){
        log.info("新增套餐：{}", winepackageDTO);
        winepackageService.saveWithWine(winepackageDTO);
        return Result.success();
    }


    /**
     * 套餐分页查询
     * @param
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("套餐分页查询")
    public Result<PageResult> page(WinepackagePageQueryDTO winepackagePageQueryDTO){
        log.info("分页查询：{}", winepackagePageQueryDTO);
        PageResult pageResult = winepackageService.pageQuery(winepackagePageQueryDTO);
        return Result.success(pageResult);
    }


    /**
     * 批量删除套餐
     * @param ids
     * @return
     */
    @DeleteMapping
    @ApiOperation("批量删除套餐")
    @CacheEvict(cacheNames = "winepackageCache", allEntries = true)
    public Result delete(@RequestParam List<Long> ids){
        log.info("批量删除套餐：{}", ids);
        winepackageService.deleteBatch(ids);
        return Result.success();
    }

    /**
     * 根据id查询套餐，用于修改页面回显数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询套餐")
    public Result<WinepackageVO> getByID(@PathVariable Long id){
        WinepackageVO winepackageVO = winepackageService.getByIdWithWine(id);
        return Result.success(winepackageVO);
    }

    /**
     * 修改套餐
     * @param winepackageDTO
     * @return
     */
    @PutMapping
    @ApiOperation("修改套餐")
    @CacheEvict(cacheNames = "winepackageCache", allEntries = true)
    public Result update(@RequestBody WinepackageDTO winepackageDTO){
        log.info("修改套餐：{}", winepackageDTO);
        winepackageService.updateWithWine(winepackageDTO);
        return Result.success();
    }

    /**
     * 启售禁售套餐
     * @param status
     * @param id
     * @return
     */
    @PostMapping("status/{status}")
    @ApiOperation("启售禁售套餐")
    @CacheEvict(cacheNames = "winepackageCache", allEntries = true)
    public Result startOrStop(@PathVariable Integer status,@RequestBody Long id){
        log.info("启售禁售套餐：{}, {}", status, id);
        winepackageService.startOrStop(status, id);
        return Result.success();
    }

}
